package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    private final CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 新增分类
     * @param categoryDTO categoryDTO
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> saveCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("执行 新增分类 接口：{}", categoryDTO);
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 删除分类
     * @param categoryId id
     * @return Result
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> deleteCategoryById(@RequestParam("id") Long categoryId){
        log.info("执行 删除分类 接口：{}", categoryId);
        categoryService.deleteCategoryById(categoryId);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO categoryDTO
     * @return Result
     */
    @PutMapping
    @ApiOperation("修改分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("执行 修改分类 接口: {}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     * @param status status
     * @param categoryId 套餐id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    @CacheEvict(cacheNames = "categoryCache",allEntries = true)
    public Result<String> startOrStopCategory(@PathVariable Integer status, @RequestParam("id") Long categoryId){
        log.info("执行 启动或禁用分类 接口: categoryId: {}, status: {}",categoryId,status);
        categoryService.startOrStopCategory(status,categoryId);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO categoryPageQueryDTO
     * @return Result
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> pageQueryCategory(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("执行 分页查询 接口: {}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQueryCategory(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询分类
     * @param type 查询数据
     * @return Result
     */
    @GetMapping("/list")
    @ApiOperation("查询分类接口")
    public Result<List<Category>> listCategory(Integer type) {
        log.info("执行 分类查询 接口: {}",type);
        List<Category> list = categoryService.listCategory(type);
        return Result.success(list);
    }
}
